﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Forms;

# region Using ArcGIS
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.esriSystem;//不用从引用加入
using ESRI.ArcGIS.Carto;//管理图层
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
# endregion

namespace popGis
{
    class PopDistributionGeometry
    {
        # region Members & Construction
        private         ILayer          m_Layer;            //操作图层
        private         string          m_PopulationField;  //图层[人口]信息字段
        private         string          m_FIDField;         //图层编码字段
        private         int             m_Year;             //图层属性数据采集年份
        public          ArrayList       m_ShapeCenter;      //图层所含图形的人口中心分布位置的ShapeCenter组列

        public PopDistributionGeometry(ILayer pLayer, string popField, int year)
        {
            m_Layer = pLayer; //if(pLayer==null) ...
            m_PopulationField = popField; m_FIDField = "FID";m_Year = year;
            m_ShapeCenter = new ArrayList();
        }
        # endregion

        //public IGeometry GetMultiPointGeometry()
        //{
        //    IPointCollection pointCollection = new MultipointClass();
        //    object _missing = Type.Missing; IPoint pPoint = null;

        //    foreach (ShapeCenter shpCenter in m_ShapeCenter)
        //    {
        //        pPoint = shpCenter.Center;
        //        pointCollection.AddPoint(pPoint, ref _missing, ref _missing);
        //    }
        //    return pointCollection as IGeometry;
        //}

        /// <summary>
        /// 获得图层所含图形的人口中心分布位置的组列 - 待修改！！！
        /// </summary>
        public  bool getShapeCenterList()
        {
            IFeatureLayer pFeatureLayer = m_Layer as IFeatureLayer;
            if (pFeatureLayer == null) return false;

            int objID; int dpop = 0;IPoint tmpPoint;
            IArea pArea = new PolygonClass();

            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            if (pFeatureClass.ShapeType != esriGeometryType.esriGeometryPolygon) 
                return false;//检查是否为面状图层
            //--------------
            ITable pTable = m_Layer as ITable;//获得属性表，下面寻找字段在属性表的序号
            int fidIndex = pTable.Fields.FindField(m_FIDField);
            int popIndex = pTable.Fields.FindField(m_PopulationField);
            int geoFieldIndex = pTable.Fields.FindField(pFeatureClass.ShapeFieldName);

            if ((fidIndex < 0) || (popIndex < 0) || (geoFieldIndex < 0))
            {
                MessageBox.Show("Field may not found!");
                return false;//如果没有字段，跳出
            }
            //-------------
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
            IFeature pFeature = pFeatureCursor.NextFeature();

            while (pFeature != null)
            {
                objID = (int)pFeature.get_Value(fidIndex);
                dpop = (int)(pFeature.get_Value(popIndex));
                //获得重心位置
                pArea = pFeature.Shape as IArea;
                tmpPoint = pArea.Centroid;

                ShapeCenter shapeCenter = new ShapeCenter(objID, tmpPoint, dpop, m_Year);

                m_ShapeCenter.Add(shapeCenter);
                pFeature = pFeatureCursor.NextFeature();
            }
            //-------------
            if (m_ShapeCenter != null) return true;
            return false;
        }


        # region 获得各多边形重心显示要素集

        /// <summary>
        /// 获得显示要素集 - 显示各多边形重心
        /// </summary>
        /// <returns></returns>
        public IElementCollection getElementCollection()
        {
            IPoint pPoint = null;
            IElementCollection pElementCollection = new ElementCollectionClass();

            foreach (ShapeCenter shpCenter in m_ShapeCenter)
            {
                pPoint = shpCenter.Center;
                IElement pElement = VisualDisplay.getIElement(40, 140, 35, 3, esriSimpleMarkerStyle.esriSMSCircle);
                pElement.Geometry = pPoint as IGeometry;
                pElementCollection.Add(pElement);
            }

            return pElementCollection;
        }



        # endregion

    }

    # region Class of ShapeCenter

    /// <summary>
    /// 人口中心分布位置
    /// describe a center(IPoint) of shape with objectID
    /// and its population (or others) in certain year
    /// </summary>
    class ShapeCenter
    {
        # region 私有成员
        private         int             oID;//地块编号
        private         IPoint          center;//重心位置
        private         int             year;//数据年份
        private         int             pop;//人口数（整数）
        # endregion

        # region 属性
        public int OID
        {
            get { return oID; }
            //set { oID = value; }
        }
        public IPoint Center
        {
            get { return center; }
            //set { center = value; }
        }
        public int Year
        {
            get { return year; }
            //set { year = value; }
        }
        public int Pop
        {
            get { return pop; }
            //set { pop = value; }
        }
        # endregion

        public ShapeCenter(int _objid,IPoint _center,int _pop, int _year)
        {
            oID = _objid; pop = _pop;
            center = _center; year = _year;
        }
    }

    # endregion
}
